<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\cms\home;
use app\cms\model\Page as Page;
use app\cms\home\Common;
use app\user\model\User as UserModel;
use app\pack\model\Pack as PackModel;
use app\pack\model\PackUser as PackUserModel;
use plugins\QiNiu\QiNiu as QiNiu;
use think\Hook;
use think\Db;
/**
 * 前台首页控制器
 * @package app\cms\admin
 */
class User extends Common
{
    // 登录
    public function dologin()
    {
        if ($this->request->isPost()) {
            $data['username'] = input('username');
            $data['password'] = input('password');
            $rememberme = isset($data['remember-me']) ? true : false;
            // 登录钩子
            $hook_result = Hook::listen('signin', $data);
            if (!empty($hook_result) && true !== $hook_result[0]) {
                $this->error($hook_result[0]);
            }
            $UserModel = new UserModel;
            $uid = $UserModel->login($data['username'], $data['password'], $rememberme);
            $User = DB::name('admin_user')->where(array('id' =>$uid))->field('role')->find();
            if ($uid) {
                // 记录行为
                if($User['role']==5){
                    $this->success('登录成功', url('cms/user/percenter'));    
                }else{
                    $this->success('登录成功', url('admin/index/index'));
                }
                
            } else {
                $this->error($UserModel->getError());
            }
        }else {
            return is_signin() ? $this->redirect('admin/index/index') : $this->fetch();
        }
        
    }
    // 个人中心
    public function percenter()
    {

       $user_auth =  session('user_auth');
       $User = DB::name('admin_user')->where(array('id' =>$user_auth['uid']))->field('third,salt,type')->find();
       $salt =  mt_rand(10000,99999);
       $token = md5($User['third'].$User['salt'].$salt);
       $project = DB::name('project')->where(array('uid' =>$user_auth['uid']))->find();
        $this->assign('project', $project);
        $this->assign('token',$token);
        $this->assign('uid',$User['third']);
        $this->assign('salt',$salt);
        $this->assign('type',$User['type']);
        return $this->fetch(); // 渲染模板
    }
    // 注册
    public function register()
    {
       // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $userdata['username'] = $data['username']; 
            $userdata['nickname'] = $data['agent_name']; 
            $userdata['role'] = 5; 
            $userdata['password'] = $data['password']; 
            $userdata['mobile'] = $data['phone']; 
            $userdata['email'] = $data['email']; 
            $userdata['status'] = 1;
            if ($user = UserModel::create($userdata)) {
                $baseUser['user_id'] = $user['id'];
                $baseUser['agent_name'] = $data['agent_name'];
                $baseUser['user_type'] = 0;
                $baseUser['status'] = 1;
                $baseUser['contact_mobile'] = $data['phone'];
                $baseUser['email'] = $data['email'];
                $baseUser['create_time'] = time();
                DB::name('user_base')->insert($baseUser);
                // 记录行为
                action_log('user_add', 'admin_user', $user['id'],'');
                $this->success('新增成功', url('index'));
            } else {
                $this->error('新增失败');
            }
        }
    }
    // 选择项目
    public function chooseproject()
    {
        session('projectid',input('id'));
        $this->success('进入项目中', url('admin/index/index'));
    }
    // 退出
    public function signout()
    {
        session(null);
        cookie('uid', null);
        cookie('signin_token', null);
        return $this->redirect('index/index');
    }
    // 创建项目
    public function createproject()
    {
       $uid =  session('user_auth.uid');
       $third = DB::name('admin_user')->where(array('id' =>$uid))->value('third');
       $packlist = DB::name('pack')->where(array('status' =>1,'is_delete'=>'0'))->select();
        $PackUser = PackUserModel::where(array('uid' =>$uid))->find();
        $pack = $PackUser['pack'];
        if($pack){
            foreach ($packlist as $k => $v) {
                if(!in_array($v['id'],$pack)){
                    unset($packlist[$k]);
                }
            }
         }else{
            $packlist = 0;
         }
       $this->assign('pack', $packlist);
       $category = DB::name('app_category')->select();
       $this->assign('category', $category);
       $this->assign('third', $third);
       return $this->fetch(); // 渲染模板
    }
    // 创建项目
    public function submitproject()
    {
       $data = $this->request->post();
       $user = session('user_auth');
       $path = $this->upload();
       $arr = json_decode($path,true);
       $data['uid'] =$user['uid'];
       $data['pic'] =$arr['path'];
       $result = DB::name('project')->insert($data);
        if($result){
            $this->success('创建成功', url('percenter'));
        } else {
            $this->error('新增失败');
        }
    }
    // 编辑项目
    public function editproject()
    {
        $id = input('id');
        $project = DB::name('project')->where(array('id' =>$id))->find();
        $uid =  session('user_auth.uid');
       $third = DB::name('admin_user')->where(array('id' =>$uid))->value('third');
       $packlist = DB::name('pack')->where(array('status' =>1,'is_delete'=>'0'))->select();
        $PackUser = PackUserModel::where(array('uid' =>$uid))->find();
        $pack = $PackUser['pack'];
        if($pack){
            foreach ($packlist as $k => $v) {
                if(!in_array($v['id'],$pack)){
                    unset($packlist[$k]);
                }
            }
         }else{
            $packlist = 0;
         }
       $this->assign('project', $project);
       $this->assign('pack', $packlist);
       $category = DB::name('app_category')->select();
       $this->assign('category', $category);
       $this->assign('third', $third);
        return $this->fetch(); // 渲染模板
    }
    // 编辑提交
    public function editPost()
    {
       $data = $this->request->post();
       $user = session('user_auth');
       $file = request()->file('avatar');
       if($file){
          $path = $this->upload();  
          $arr = json_decode($path,true);
          $data['pic'] =$arr['path'];
       }
       $result = DB::name('project')->where(array('uid' =>$user['uid']))->update($data);
        if($result){
            $this->success('更新成功', url('percenter'));
        } else {
            $this->success('更新成功');
        }
    }
    public function upload(){
        $file = request()->file('avatar');
        $QiNiu = new QiNiu;
        $result = $QiNiu->uploadAttachment($file,['module'=>'project','from'=>'']);
        return $result;
    }
    /**
     * 数据签名认证
     * @param array $data 被认证的数据
     * @author 蔡伟明 <314013107@qq.com>
     * @return string 签名
     */
    public function dataAuthSign($data = [])
    {
        // 数据类型检测
        if(!is_array($data)){
            $data = (array)$data;
        }
        // 排序
        ksort($data);
        // url编码并生成query字符串
        $code = http_build_query($data);
        // 生成签名
        $sign = sha1($code);
        return $sign;
    }
}